<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr,
		errorModal
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				title:'',
				dutyMultiOffIdCacheKey:'mar-gzx-biddingWork-title2-dutyMultiOffId-CacheKey',
				evaOffIdCacheKey:'mar-gzx-biddingWork-title2-evaOffId-CacheKey',
				defaultDataCache:'mar-gzx-biddingWork-title2-defaultData-CacheKey',
				evaMultiOffDefaultDataCache:'mar-gzx-biddingWork-title2-evaMultiOffdefaultData-CacheKey',
				busCode:'',
				bodyUserId:'',
				canEdit:true,
				evaMultiOffCacheKey:'mar-gzx-biddingWork-title2-evaMultiOff-CacheKey'
			})
		},
		methods:{
			clearDutyMultiOffId(){
				let _that = this
				_that.$refs.autoform.clear("title2","dutyMultiOffId")
				uni.removeStorage({
					key:_that.defaultDataCache
				})
			},
			clearEvaMultiOffId(){
				let _that = this
				_that.$refs.autoform.clear("title2","evaMultiOffId")
				uni.removeStorage({
					key:_that.evaMultiOffDefaultDataCache
				})
			},
			inputMaxScore(val){
				let _that = this
				if(Number(val) > 10){
					_that.$refs.autoform.formdata.maxScore = 10
				}
				if(Number(val) < 0){
					_that.$refs.autoform.formdata.maxScore = 0
				}
				if( Number(val) >= 0 &&  Number(val)<= 10){
					_that.$refs.autoform.formdata.maxScore = Number(val).toFixed(1)
				}
			},
			getMultiEvaUserResponse(userIdPosId){
				let _that = this
				_that.om.request(
					"workEvaluateConfig/getMultiEvaUserResponse",
					{
						busCode:_that.busCode,
						userIdPosId: userIdPosId,
						bodyUserId:_that.bodyUserId
					}
				).then(res=>{
					if(_that.canEdit){
						let objArr = []
						let ids = res.value.evaMultiOffId.split(',')
						let names = res.value.evaMultiOffName.split(',')
						let posNames = res.value.evaMultiPosName.split(',')
						for(let index in ids){
							let obj = {
								checked: true,
								id: null,
								instId: null,
								instName: '',
								isUser: true,
								posId: null,
								posInstName:'',
								posName: '',
								username: ''
							}
							let userPosId = ids[index].split('/')
							obj.id = userPosId[0]
							obj.posId = userPosId[1]
							obj.username = names[index]
							obj.posName = posNames[index]
							objArr.push(obj)
						}
						_that.$refs.autoform.formdata.evaMultiPosName = res.value.evaMultiPosName
						uni.setStorage({
							key:_that.evaMultiOffDefaultDataCache,
							data:objArr
						})
					}
					_that.$refs.autoform.formdata.evaMultiOffId = res.value.evaMultiOffId
					_that.$refs.autoform.formdata.evaMultiOffName = res.value.evaMultiOffName
				})
			},
			onselectStartDate(val){
				let _that = this
				let title1 = _that.$refs.autoform.getEditorTitle("title2","startDate")
				let title2 = _that.$refs.autoform.getEditorTitle("title2","endDate")
				if(_that.$refs.autoform.formdata.endDate){
					if(moment(val).isAfter(_that.$refs.autoform.formdata.endDate)){
						errorModal(_that,'提示信息',title1+"不能大于"+title2,()=>{
							_that.$refs.autoform.clear("title2","startDate")
						})
					}
				}
			},
			onselectEndDate(val){
				let _that = this
				let title1 = _that.$refs.autoform.getEditorTitle("title2","startDate")
				let title2 = _that.$refs.autoform.getEditorTitle("title2","endDate")
				if(_that.$refs.autoform.formdata.startDate){
					if(moment(val).isBefore(_that.$refs.autoform.formdata.startDate)){
						errorModal(_that,'提示信息',title2+"不能小于"+title1,()=>{
							_that.$refs.autoform.clear("title2","endDate")
						})
					}
				}
			},
			onselectReportEvaCode(item){
				let _that = this
				if(item.code == '39758'){
					if(_that.canEdit){
						_that.$refs.autoform.changeEditor("title2","evaMultiOffId",'type','linkbox')
						_that.$refs.autoform.changeEditor("title2","evaMultiOffId", {
							'link':'../../public/searchUser/searchUser',
							linkParam: {
								cachename: _that.evaMultiOffCacheKey,
								defaultDataCache:_that.evaMultiOffDefaultDataCache,
								isNew:true,
								isUserPos:true,
								placeholder: urlUtil.userPageUrl().prompt,
								pagination: true
							}
						});
						_that.$refs.autoform.changeEditor("title2","maxScore","type","numberbox")
					}
					_that.$refs.autoform.formdata.maxScore = 10
					_that.$refs.autoform.changeEditor("title2","evaOffId","type","linkbox")
					
					if(_that.$refs.autoform.formdata.dutyMultiOffId){
						_that.getMultiEvaUserResponse(_that.$refs.autoform.formdata.dutyMultiOffId)
					}
				}else{
					_that.$refs.autoform.changeEditor("title2","evaMultiOffId",'type','labelbox')
					_that.$refs.autoform.clear("title2",'evaOffId')
					_that.$refs.autoform.clear("title2",'maxScore')
					_that.$refs.autoform.changeEditor("title2","evaOffId","type","labelbox")
					_that.$refs.autoform.changeEditor("title2","maxScore","type","labelbox")
					_that.$refs.autoform.clear("title2","evaMultiOffId")
				}
			},
			clearReportEvaCode(){
				let _that = this
				_that.$refs.autoform.clear("title2","evaMultiOffId")
				_that.$refs.autoform.clear("title2",'reportEvaCode')
				_that.$refs.autoform.clear("title2",'evaOffId')
				_that.$refs.autoform.clear("title2",'maxScore')
				_that.$refs.autoform.changeEditor("title2","evaOffId","type","labelbox")
				_that.$refs.autoform.changeEditor("title2","evaMultiOffId","type","labelbox")
				_that.$refs.autoform.changeEditor("title2","maxScore","type","labelbox")
				uni.removeStorage({
					key:_that.evaMultiOffDefaultDataCache
				})
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request('tsysdict/getListByCode', { code: 39757 }),
					_that.om.request('dictZero/findBooleanDtoList', {}),
					_that.om.request('dictSource/loadCombo', { sourceId: 289,categoryId: 8566 }),
					_that.om.request('dictSource/loadCombo', { sourceId: 283,categoryId: 6116 })
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title2","reportEvaCode","data",configSelectData(res[0],'code','dicname'))
					_that.$refs.autoform.initSelectIndex("title2","reportEvaCode")
					_that.$refs.autoform.changeEditor("title2","keyItemFlag","data",configSelectData(res[1],'value','name'))
					_that.$refs.autoform.initSelectIndex("title2","keyItemFlag")
					_that.$refs.autoform.changeEditor("title2","importanceCode","data",configSelectData(res[2],'code','text'))
					_that.$refs.autoform.initSelectIndex("title2","importanceCode")
					_that.$refs.autoform.changeEditor("title2","workCateCode","data",configSelectData(res[3],'code','text'))
					_that.$refs.autoform.initSelectIndex("title2","workCateCode")
				})
				_that.$nextTick(function(){
					if(_that.$refs.autoform.formdata.reportEvaCode == '39758'){
						_that.$refs.autoform.changeEditor("title2","evaOffId","type","linkbox")
						_that.$refs.autoform.changeEditor("title2","maxScore","type","numberbox")
					}else{
						_that.$refs.autoform.clear("title2",'evaOffId')
						_that.$refs.autoform.clear("title2",'maxScore')
						_that.$refs.autoform.changeEditor("title2","evaOffId","type","labelbox")
						_that.$refs.autoform.changeEditor("title2","maxScore","type","labelbox")
					}
				})
			}
			
		},
		onUnload(){
			let _that = this
			uni.removeStorage({
				key:_that.defaultDataCache
			})
			
		},
		onLoad(e) {
			let _that = this
			let arr = [
				{
					url:"dataDict/getIntByCode",
					param:{
						code:'DD021025001'
					}
				}
			]
			subOnLoad(_that, e, function(ajaxData) {
				if(e.title){
					_that.title = e.title
				}
				if(e.busCode){
					_that.busCode = e.busCode
				}
				if(e.generalResponsibleUserId){
					_that.bodyUserId = e.generalResponsibleUserId
				}
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							changeEditor("title2","dutyMultiOffId",{
								'link': '../../public/searchUser/searchUser',
								"linkParam":{
									isNew:true,
									isUserPos:true,
									cachename:_that.dutyMultiOffIdCacheKey,
									defaultDataCache:_that.defaultDataCache,
								}
							},null,_that.autoformdata)
							if(_that.formdata.dutyMultiOffId){
								let objArr = []
								let ids = _that.formdata.dutyMultiOffId.split(',')
								let names = _that.formdata.dutyMultiOffName.split(',')
								let posNames = _that.formdata.dutyMultiPosName.split(',')
								for(let index in ids){
									let obj = {
										checked: true,
										id: null,
										instId: null,
										instName: '',
										isUser: true,
										posId: null,
										posInstName:'',
										posName: '',
										username: ''
									}
									let userPosId = ids[index].split('/')
									obj.id = userPosId[0]
									obj.posId = userPosId[1]
									obj.username = names[index]
									obj.posName = posNames[index]
									objArr.push(obj)
								}
								uni.setStorage({
									key:_that.defaultDataCache,
									data:objArr
								})
							}
							changeEditor("title2","evaOffId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										oldValue: _that.formdata.evaOffId
									}),
									url:'orgInstPosUserOfficial/findListByOrg',
									namefield:'userPosName',
									initload:true,
									searchfield:'q',
									cachename:_that.evaOffIdCacheKey
								}
							},null,_that.autoformdata)
							if(ajaxData[0].value + '' == '0'){
								if(_that.formdata.reportEvaCode == '39758'){
									changeEditor("title2","evaMultiOffId", {
										'link':'../../public/searchUser/searchUser',
										linkParam: {
											cachename: _that.evaMultiOffCacheKey,
											defaultDataCache:_that.evaMultiOffDefaultDataCache,
											isNew:true,
											isUserPos:true,
											placeholder: urlUtil.userPageUrl().prompt,
											pagination: true
										}
									}, null, _that.autoformdata);
								}else{
									changeEditor("title2","evaMultiOffId",'type','labelbox',_that.autoformdata)
								}
							}else{
								_that.canEdit = false
								changeEditor("title2","evaMultiOffId",'type','labelbox',_that.autoformdata)
							}
							_that.initload()
						}
					})
				}
			},arr)
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.dutyMultiOffIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							uni.setStorage({
								key:_that.defaultDataCache,
								data:d
							})
							if(d.length){
								let idList = []
								let nameList = []
								let dutyMultiOffId = ''
								let posNameList = []
								for(let index in d){
									idList.push(d[index].id + '/' + d[index].posId)
									nameList.push(d[index].username)
									posNameList.push(d[index].posName)
								}
								let idStr = idList.map(e =>e).join(",")
								let nameStr = nameList.map(e =>e).join(",");
								let posNameStr = posNameList.map(e =>e).join(",");
								
								formdata.dutyMultiOffId = idStr
								formdata.dutyMultiOffName = nameStr
								formdata.dutyMultiPosName = posNameStr
							}
							if(formdata.reportEvaCode && formdata.reportEvaCode == '39758' ){
								_that.getMultiEvaUserResponse(formdata.dutyMultiOffId)
							}
							uni.removeStorage({
								key:_that.dutyMultiOffIdCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.evaMultiOffCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data;
							uni.setStorage({
								key:_that.evaMultiOffDefaultDataCache, 
								data:d
							})
							let formdata = _that.$refs.autoform.formdata;
							if(d.length){
								let idList = []
								let nameList = []
								let posNameList = []
								for(let index in d){
									idList.push(d[index].id + '/' + d[index].posId)
									nameList.push(d[index].username)
									posNameList.push(d[index].posName)
								}
								
								let idStr = idList.map(e =>e).join(",")
								let nameStr = nameList.map(e =>e).join(",");
								let posNameStr = posNameList.map(e =>e).join(",");
								
								formdata.evaMultiOffId = idStr
								formdata.evaMultiOffName = nameStr
								formdata.evaMultiPosName = posNameStr
							}else{
								_that.$refs.autoform.clear("title2","evaMultiOffId")
							}
							uni.removeStorage({
								key: _that.evaMultiOffCacheKey
							});
						}
					}
				})
				uni.getStorage({
					key:_that.evaOffIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.evaOffId = d.id
							formdata.evaOffName = d.userPosName
							_that.$refs.autoform.changeEditor("title2","evaOffId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										oldValue: d.id
									}),
									url:'orgInstPosUserOfficial/findListByOrg',
									namefield:'userPosName',
									initload:true,
									searchfield:'q',
									cachename:_that.evaOffIdCacheKey
								}
							})
							uni.removeStorage({
								key:_that.evaOffIdCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
